//
//  LXLWelcomeViewController.swift
//  Weibo-YQY-test
//
//  Created by Lang on 10/16/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import UIKit
import SDWebImage

class LXLWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        // 设置 headView 的位置
        headImageView.centerX = SCREENW * 0.5
        headImageView.y = 200
        
        //添加headView
        view.addSubview(headImageView)
        //添加infoLabel
        view.addSubview(infoLabel)
    }
    
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
       
        
        
        //设置动画
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                self.headImageView.y = 100
            }) { (finish) -> Void in
                printLog("动画执行完毕")
                self.infoLabel.centerX = self.headImageView.centerX
                self.infoLabel.y = CGRectGetMaxY(self.headImageView.frame)+5
                
                //设置初始的透明度
                self.infoLabel.alpha = 0
                
                //显示
                self.infoLabel.hidden = false
                
                //设置显示动画
                 UIView.animateWithDuration(1, animations: { () -> Void in
                        self.infoLabel.alpha = 1
                    }, completion: { (finish) -> Void in
                        //动画全部执行结束，跳转界面
                        self.performSelector("toHome", withObject: nil, afterDelay: 1)
                 })
                
        }
        
    }
    
    
    // 跳转到首页
    @objc private func toHome(){
        //获取到项目的 appDelegate
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        //取出 window, 切换跟控制
        delegate.window?.rootViewController = LXLTabBarController()
    }
    

    // 头像的懒加载
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView()
        //设置大小
        imageView.size = CGSizeMake(90, 90)
        
        //设置 imageView 的圆角
        imageView.layer.cornerRadius = imageView.height * 0.5
        // 允许裁剪
        imageView.layer.masksToBounds = true
        // 图片的边线
        imageView.layer.borderWidth = 2
        imageView.layer.borderColor = UIColor.darkGrayColor().CGColor
        
        //用户的头像地址
        if LXLUserAccount.loadUserAccount()?.avatar_large != nil {
            let url = NSURL(string: LXLUserAccount.loadUserAccount()!.avatar_large!)
            //通过 sdwebimage 加载图片
            imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        
        return imageView
        
    }()
    
    // 欢迎文字的控件
    private lazy var infoLabel: UILabel = {
        let label = UILabel()
        // 文字
        label.text = "欢迎回来"
        //字体颜色
        label.textColor = UIColor.blackColor()
        //字体大小
        label.font = UIFont.systemFontOfSize(16)
        
        //大小
        label.sizeToFit()
        
        label.hidden = true
        
        return label
    }()
}
